<template>
  <view class="video-result-container">
    <!-- 视频直接显示在背景上 -->
    <view class="video-section">
      <video 
        :src="videoUrl" 
        class="video-player"
        controls
        autoplay
        muted
        show-center-play-btn
        show-play-btn
        show-fullscreen-btn
      ></video>
    </view>
    
    <!-- 底部操作区域 -->
    <view class="bottom-actions">
      <button class="download-btn" @click="downloadVideo">
         保存到相册
      </button>
      <button class="back-btn" @click="goBack">
        返回
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const videoUrl = ref('')

onMounted(() => {
  const pages = getCurrentPages()
  const current = pages[pages.length - 1]
  const { options } = current
  
  if (options?.videoUrl) {
    videoUrl.value = decodeURIComponent(options.videoUrl)
  }
})



function goBack() {
  uni.navigateBack()
}

// 下载视频到相册
function downloadVideo() {
  if (!videoUrl.value) {
    uni.showToast({
      title: '视频地址无效',
      icon: 'none'
    })
    return
  }
  
  // 如果是网络URL，需要先下载到本地
  if (videoUrl.value.startsWith('http')) {
    uni.showLoading({
      title: '下载中...'
    })
    
    uni.downloadFile({
      url: videoUrl.value,
      success: (res) => {
        uni.hideLoading()
        if (res.statusCode === 200) {
          // 下载成功，保存到相册
          saveVideoToAlbum(res.tempFilePath)
        } else {
          uni.showToast({
            title: '下载失败',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        uni.hideLoading()
        console.error('下载视频失败:', err)
        uni.showToast({
          title: '下载失败',
          icon: 'none'
        })
      }
    })
  } else {
    // 本地文件直接保存
    saveVideoToAlbum(videoUrl.value)
  }
}

// 保存视频到相册
function saveVideoToAlbum(filePath) {
  uni.saveVideoToPhotosAlbum({
    filePath: filePath,
    success: () => {
      uni.showToast({
        title: '保存成功',
        icon: 'success'
      })
    },
    fail: (err) => {
      console.error('保存视频失败:', err)
      let errorMsg = '保存失败'
      
      // 根据错误码显示具体错误信息
      if (err.errCode) {
        switch (err.errCode) {
          case 1101001:
            errorMsg = '用户取消保存'
            break
          case 1101003:
            errorMsg = '文件不存在'
            break
          case 1101005:
            errorMsg = '未获取相册权限'
            break
          case 1101006:
            errorMsg = '视频保存失败'
            break
          default:
            errorMsg = `保存失败 (${err.errCode})`
        }
      }
      
      uni.showToast({
        title: errorMsg,
        icon: 'none'
      })
    }
  })
}
</script>

<style lang="scss">
.video-result-container {
  min-height: 100vh;
  background: #1a1a1a;
  display: flex;
  flex-direction: column;
}

.video-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.video-player {
  width: 100%;
  height: 100%;
  max-height: 70vh;
  border-radius: 12px;
  background: #000;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px;
  background: linear-gradient(transparent, #1a1a1a 70%);
  display: flex;
  gap: 15px;
}

.download-btn {
  flex: 1;
  background: linear-gradient(135deg, #ff0050, #ff4081);
  color: #ffffff;
  border: none;
  border-radius: 25px;
  padding: 15px 20px;
  font-size: 16px;
  font-weight: 600;
  font-family: 'Fugaz One', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  transition: all 0.3s ease;
  
  &:active {
    transform: translateY(2px);
    background: linear-gradient(135deg, #e6004a, #e63972);
  }
}

.back-btn {
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 25px;
  padding: 15px 25px;
  font-size: 16px;
  font-weight: 600;
  font-family: 'Fugaz One', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  transition: all 0.3s ease;
  
  &:active {
    transform: translateY(2px);
    background: rgba(255, 255, 255, 0.2);
  }
}
</style>
